/*
 * Copyright (C) 2011 JiangHongTiao
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove.jianghongtiao.paris.core.search;

import org.apache.log4j.Logger;
import sk.lieskove.jianghongtiao.common.utils.PropertiesUtils;
import sk.lieskove.jianghongtiao.websearch.ThreadFullWebSearch;
import sk.lieskove.jianghongtiao.websearch.service.SearchQuery;
import sk.lieskove.jianghongtiao.websearch.service.SearchQueryImpl;
import sk.lieskove.jianghongtiao.websearch.persistence.WebSearchResponse;
import sk.lieskove.jianghongtiao.common.enums.searchserviceenum.SearchServiceEnum;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author xjuraj e-mail: jjurco.sk_gmail.com
 */
public class ThreadPeopleSearch implements Serializable, PeopleNameListSearch {

    private transient Logger log = Logger.getLogger(ThreadPeopleSearch.class);
    private transient PropertiesUtils pu = new PropertiesUtils(ThreadPeopleSearch.class);

    private final int MAX_THREADS = Integer.parseInt(
            pu.getProperty("max-threads", "2"));
    @Override
    public List<WebSearchResponse> searchForPeople(List<String> personList) {
        List<WebSearchResponse> results = new ArrayList<WebSearchResponse>();
        List<ThreadFullWebSearch> threadFWSList = new ArrayList<ThreadFullWebSearch>();
        for (String person : personList) {
            if (person.equals("")) {
                continue;
            }
            ThreadFullWebSearch fullWebSearch =
                    new ThreadFullWebSearch("Thread full web search: " + person);
            SearchQuery query = new SearchQueryImpl();
            query.addWord(person);
            fullWebSearch.search(query,
                    SearchServiceEnum.WEBSEARCH_BING,
                    SearchServiceEnum.WEBSEARCH_GOOGLE);
            threadFWSList.add(fullWebSearch);
            fullWebSearch.start();
            //wait for threads
            if (threadFWSList.size() >= MAX_THREADS) {
                for (ThreadFullWebSearch search : threadFWSList) {
                    try {
                        search.join();
                    } catch (InterruptedException ex) {
                        log.warn("Thread was interrupted! " + search.getName());
                    }
                    results.addAll(search.getResults());
                }
                threadFWSList.clear();
                //manually call garbage collector to be sure all remained 
                //objects disappear
                System.gc();
            }
        }
        return results;
    }

}
